

#include "Losu_Std.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<fstream>
using namespace std;
static int mylength(string str){
    int rt=0;
    for(int i=0;i<str.length();i++){
        if(str[i]>=(char)0xe2&&str[i]<=(char)0xef)  i=i+2;
        else if(str[i]==(char)0xf0) i=i+3;
        rt++;
    }
    return rt;
}
static string mymid(string str,int pos,int len){
    int npos=1,nlen=0;
    int i;
    string rt;
    for( i=0;npos!=pos;i++){
        if(i>=str.length())
            return "";
        if(str[i]>=(char)0xe2&&str[i]<=(char)0xef)  i=i+2;
        else if(str[i]==(char)0xf0) i=i+3;
        npos++;
    }
    for(;nlen!=len;i++){
        if(i>=str.length())
            nlen = len-1;
        rt+=str[i];
        if(str[i]>=(char)0xe2&&str[i]<=(char)0xef){
            i++;rt+=str[i];
            i++;rt+=str[i];
        }else if(str[i]==(char)0xf0){
            i++;rt+=str[i];
            i++;rt+=str[i];
            i++;rt+=str[i];
        }
        
        nlen++;
    }
    return rt;
    

}
static string mytoupper(string str){
    string rt;
    for(int i=0;i<str.size();i++){
        if(str[i]>='a'&&str[i]<='z')
            rt+=(str[i]-32);
        else if(str[i]>=(char)0xe2&&str[i]<=(char)0xef){
            rt+=str[i];
            i++;rt+=str[i];
            i++;rt+=str[i];
        }
        else if(str[i]==(char)0xf0){
            rt+=str[i];
            i++;rt+=str[i];
            i++;rt+=str[i];
            i++;rt+=str[i];
        }else rt+= str[i];
    }
    return rt;
}
static string mytolower(string str){
    string rt;
    for(int i=0;i<str.size();i++){
        if(str[i]>='A'&&str[i]<='Z')
            rt+=(str[i]+32);
        else if(str[i]>=(char)0xe2&&str[i]<=(char)0xef){
            rt+=str[i];
            i++;rt+=str[i];
            i++;rt+=str[i];
        }
        else if(str[i]==(char)0xf0){
            rt+=str[i];
            i++;rt+=str[i];
            i++;rt+=str[i];
            i++;rt+=str[i];
        }else rt+= str[i];
    }
    return rt;
}
static string mymiror(string str){
    string rt;
    for(int i=0;i<str.length();i++){
        if(str[i]>=(char)0xe2&&str[i]<=(char)0xef){
            rt = str.substr(i,1)+str.substr(i+1,1)+str.substr(i+2,1)+rt;
            i=i+2;
        }else if(str[i]==(char)0xf0){
            rt = str.substr(i,1)+str.substr(i+1,1)+str.substr(i+2,1)+str.substr(i+3,1)+rt;
            i=i+3;
        }else rt = str[i]+rt;
    }
    return rt;
}
static string mid(string a,long start_pos,long len_pos){
    if (start_pos > a.length()){
        return "";
    }
	return a.substr(start_pos - 1,len_pos);
}
static string myreplace(string src,string old_value,string new_value){
    long i;
	string rep;
	i = 1;
	while(1){
		if (mid(src,i,old_value.length()) == old_value ){
			i = i + old_value.length() -1 ;
			rep = rep + new_value ;
		}else{
			rep = rep + mid(src,i,1) ;
		}
		i++;
		if (i > src.length()){
			rep = rep + mid(src,i,1) ;
			break;
		}
	}
	return rep;
}


extern "C"{
extern LNI_return rt;
extern string rtstr;
LNI_return Els_String_len(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    string str = a.str[3];
        int r=0;
        for(int i=0;i<str.length();i++){
            if(str[i]>=(char)0xe2&&str[i]<=(char)0xef)  i=i+2;
            else if(str[i]==(char)0xf0) i=i+3;
            r++;
        }
        
        rt.num = r;
        return rt;
}
LNI_return Els_String_asciilen(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    rt.num = strlen(a.str[3]);
    return rt;
}
LNI_return Els_String_find(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    string str = a.str[3],obj=a.str[4];
    
    for(int i = 1;i<=str.length()-obj.length()+1;i++){
        if(mymid(str,i,mylength(obj))==obj){
            rt.num =i;
            return rt;
        }
    }
    rt.num = 0;
    return rt;
}
LNI_return Els_String_replace(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    rtstr   = myreplace(a.str[3],a.str[4],a.str[5]);
    rt.str  = (char*)rtstr.c_str();
    return rt;
}
LNI_return Els_String_mid(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    rtstr   = mymid(a.str[3],(int)a.num[4],(int)a.num[5]);
    rt.str  = (char*)rtstr.c_str();    
    return rt;
}
LNI_return Els_String_reverse(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    string str = a.str[3];
    rtstr=""; 
    for(int i=0;i<str.length();i++){
        if(str[i]>=(char)0xe2&&str[i]<=(char)0xef){
            rtstr = str.substr(i,1)+str.substr(i+1,1)+str.substr(i+2,1)+rtstr;
            i=i+2;
        }else if(str[i]==(char)0xf0){
            rtstr = str.substr(i,1)+str.substr(i+1,1)+str.substr(i+2,1)+str.substr(i+3,1)+rtstr;
            i=i+3;
        }else rtstr = str[i]+rtstr;
    }
    rt.str = (char*)(rtstr.c_str());
    return rt;
}
LNI_return Els_String_toupper(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    string str= a.str[3];
    rtstr = "";
        for(int i=0;i<str.size();i++){
            if(str[i]>='a'&&str[i]<='z')
                rtstr+=(str[i]-32);
            else if(str[i]>=(char)0xe2&&str[i]<=(char)0xef){
                rtstr+=str[i];
                i++;rtstr+=str[i];
                i++;rtstr+=str[i];
            }else if(str[i]==(char)0xf0){
                rtstr+=str[i];
                i++;rtstr+=str[i];
                i++;rtstr+=str[i];
                i++;rtstr+=str[i];
            }else rtstr+= str[i];
        }
        rt.str = (char*)rtstr.c_str();
        return rt;
}
LNI_return Els_String_tolower(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    string str= a.str[3];
    rtstr = "";
        for(int i=0;i<str.size();i++){
            if(str[i]>='A'&&str[i]<='Z')
                rtstr+=(str[i]+32);
            else if(str[i]>=(char)0xe2&&str[i]<=(char)0xef){
                rtstr+=str[i];
                i++;rtstr+=str[i];
                i++;rtstr+=str[i];
            }else if(str[i]==(char)0xf0){
                rtstr+=str[i];
                i++;rtstr+=str[i];
                i++;rtstr+=str[i];
                i++;rtstr+=str[i];
            }else rtstr+= str[i];
        }
        rt.str = (char*)rtstr.c_str();
        return rt;
}
LNI_return Els_String_createstr(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    rtstr = "";
    for(int i =0;i<a.num[4];i++)
        rtstr += a.str[3];

    rt.str = (char*)rtstr.c_str();
    return rt;
}
LNI_return Els_String_countnum(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
        string str = a.str[3],obj=a.str[4];
        rt.num=0;
        for(int i = 1;i<=mylength(str);i++){
            if(mymid(str,i,mylength(obj))==obj) 
                rt.num ++;
        }
    return rt;
}

LNI_return Els_String_asciimid(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    string str = a.str[3];
    rtstr = str.substr((int)a.num[4]-1,(int)a.num[5]);
    rt.str = (char*)rtstr.c_str();
    return rt;
}
LNI_return Els_String_ascii(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    rt.num = (double)((int)((char)((a.str[3])[0])));
    return rt;
}
LNI_return Els_String_char(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    rtstr = (char)((int)(a.num[3]));
    rt.str = (char*)(rtstr.c_str());
    return rt;
}
}